<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="数据日期" prop="refDate">
        <el-date-picker
          v-model="queryParams.refDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="小时" prop="refHour">
        <el-select
          v-model="queryParams.refHour"
          clearable
          class="!w-240px"
        >
          <el-option label="1时" value="000" />
          <el-option label="2时" value="100" />
          <el-option label="3时" value="200" />
          <el-option label="4时" value="300" />
          <el-option label="5时" value="400" />
          <el-option label="6时" value="500" />
          <el-option label="7时" value="600" />
          <el-option label="8时" value="700" />
          <el-option label="9时" value="800" />
          <el-option label="10时" value="900" />
          <el-option label="11时" value="1000" />
          <el-option label="12时" value="1100" />
          <el-option label="13时" value="1200" />
          <el-option label="14时" value="1300" />
          <el-option label="15时" value="1400" />
          <el-option label="16时" value="1500" />
          <el-option label="17时" value="1600" />
          <el-option label="18时" value="1700" />
          <el-option label="19时" value="1800" />
          <el-option label="20时" value="1900" />
          <el-option label="21时" value="2000" />
          <el-option label="22时" value="2100" />
          <el-option label="23时" value="2200" />
          <el-option label="24时" value="2300" />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="数据类型" prop="dataTypeCode">
        <el-select
          v-model="queryParams.dataTypeCode"
          clearable
          class="!w-240px"
        >
          <el-option label="图文群发每日数据" value="1" />
          <el-option label="图文统计数据" value="3" />
          <el-option label="图文统计分时数据" value="4" />
          <el-option label="图文分享转发数据" value="5" />
          <el-option label="图文分享转发分时数据" value="6" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['integration:weixin-article-analysis:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['integration:weixin-article-analysis:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="数据日期" align="center" prop="refDate" />
      <el-table-column label="小时" align="center" prop="refHour">
        <template #default="scope">
          <span v-if="scope.row.refHour == '000'">1时</span>
          <span v-else-if="scope.row.refHour == '100'">2时</span>
          <span v-else-if="scope.row.refHour == '200'">3时</span>
          <span v-else-if="scope.row.refHour == '300'">4时</span>
          <span v-else-if="scope.row.refHour == '400'">5时</span>
          <span v-else-if="scope.row.refHour == '500'">6时</span>
          <span v-else-if="scope.row.refHour == '600'">7时</span>
          <span v-else-if="scope.row.refHour == '700'">8时</span>
          <span v-else-if="scope.row.refHour == '800'">9时</span>
          <span v-else-if="scope.row.refHour == '900'">10时</span>
          <span v-else-if="scope.row.refHour == '1000'">11时</span>
          <span v-else-if="scope.row.refHour == '1100'">12时</span>
          <span v-else-if="scope.row.refHour == '1200'">13时</span>
          <span v-else-if="scope.row.refHour == '1300'">14时</span>
          <span v-else-if="scope.row.refHour == '1400'">15时</span>
          <span v-else-if="scope.row.refHour == '1500'">16时</span>
          <span v-else-if="scope.row.refHour == '1600'">17时</span>
          <span v-else-if="scope.row.refHour == '1700'">18时</span>
          <span v-else-if="scope.row.refHour == '1800'">19时</span>
          <span v-else-if="scope.row.refHour == '1900'">20时</span>
          <span v-else-if="scope.row.refHour == '2000'">21时</span>
          <span v-else-if="scope.row.refHour == '2100'">22时</span>
          <span v-else-if="scope.row.refHour == '2200'">23时</span>
          <span v-else-if="scope.row.refHour == '2300'">24时</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="分享人数" align="center" prop="shareUser" />
      <el-table-column label="分享次数" align="center" prop="shareCount" />
      <el-table-column label="数据类型" align="center" prop="dataTypeCode">
        <template #default="scope">
          <span v-if="scope.row.dataTypeCode == '1'">图文群发每日数据</span>
          <span v-else-if="scope.row.dataTypeCode == '3'">图文统计数据</span>
          <span v-else-if="scope.row.dataTypeCode == '4'">图文统计分时数据</span>
          <span v-else-if="scope.row.dataTypeCode == '5'">图文分享转发数据</span>
          <span v-else-if="scope.row.dataTypeCode == '6'">图文分享转发分时数据</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['integration:weixin-article-analysis:update']"
          >
            编辑
          </el-button>
          <el-button
              link
              type="primary"
              @click="openForm('detail', scope.row.id)"
              v-hasPermi="['integration:weixin-article-analysis:query']"
          >
            查看
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['integration:weixin-article-analysis:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <WeixinArticleAnalysisForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { WeixinArticleAnalysisApi, WeixinArticleAnalysisVO } from '@/api/integration/weixinarticleanalysis'
import WeixinArticleAnalysisForm from './WeixinArticleAnalysisForm.vue'

/** 微信文章分析 列表 */
defineOptions({ name: 'WeixinArticleAnalysis' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<WeixinArticleAnalysisVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  refDate: [],
  refHour: undefined,
  msgid: undefined,
  title: undefined,
  intPageReadUser: undefined,
  intPageReadCount: undefined,
  oriPageReadUser: undefined,
  oriPageReadCount: undefined,
  shareUser: undefined,
  shareCount: undefined,
  addToFavUser: undefined,
  addToFavCount: undefined,
  dataTypeCode: undefined,
  dataType: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WeixinArticleAnalysisApi.getWeixinArticleAnalysisPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await WeixinArticleAnalysisApi.deleteWeixinArticleAnalysis(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WeixinArticleAnalysisApi.exportWeixinArticleAnalysis(queryParams)
    download.excel(data, '微信文章分析.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
